/*!
    \file    gd32w51x_it.c
    \brief   interrupt service routines

    \version 2021-03-25, V1.0.0, firmware for GD32W51x
*/

/*
    Copyright (c) 2021, GigaDevice Semiconductor Inc.

    Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors
       may be used to endorse or promote products derived from this software without
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/

#include "gd32w51x_it.h"
#include "stdio.h"
#include "main.h"
#include "drv_usb_hw.h"
#include "usbd_msc_core.h"
#include "drv_usbd_int.h"
#include "sdcard.h"
#include "DevicesTime.h"
#include "DevicesUart.h"
#include "DevicesQueue.h"
#include "DevicesLed.h"
#include "DevicesSoftTimer.h"
#include "DevicesIFRP.h"
#include "DevicesIIS.h"
#include "systick.h"
#include "UserTask.h"

/*!
    \brief      this function handles NMI exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void NMI_Handler(void)
{
}

/*!
    \brief      this function handles HardFault exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
// void HardFault_Handler(void)
// {
//     printf("\r\nHardFault_Handler.\r\n");
//     /* if Hard Fault exception occurs, go to infinite loop */
//     while(1){
//     }
// }

/*!
    \brief      this function handles MemManage exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void MemManage_Handler(void)
{
    printf("\r\nMemManage_Handler.\r\n");
    /* if Memory Manage exception occurs, go to infinite loop */
    while(1){
    }
}

/*!
    \brief      this function handles BusFault exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void BusFault_Handler(void)
{
    printf("\r\nBusFault_Handler.\r\n");
    /* if Bus Fault exception occurs, go to infinite loop */
    while(1){
    }
}

/*!
    \brief      this function handles UsageFault exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void UsageFault_Handler(void)
{
    printf("\r\nUsageFault_Handler.\r\n");
    /* if Usage Fault exception occurs, go to infinite loop */
    while(1){
    }
}

/*!
    \brief      this function handles SVC exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
// void SVC_Handler(void)
// {
// }

/*!
    \brief      this function handles DebugMon exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void DebugMon_Handler(void)
{
}

/*!
    \brief      this function handles PendSV exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
// void PendSV_Handler(void)
// {
// }

/*!
    \brief      this function handles SysTick exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
// void SysTick_Handler(void)
// {
//    delay_decrement();
// }

/*!
    \brief      This function handles TIMER interrupt request.
    \param[in]  none
    \param[out] none
    \retval     none
*/
void TIMER1_IRQHandler(void)
{
    if(timer_interrupt_flag_get(TIMER1, TIMER_INT_UP) != RESET)
    {
        timer_interrupt_flag_clear(TIMER1, TIMER_INT_UP);
    }
}

/*!
    \brief      This function handles TIMER interrupt request.
    \param[in]  none
    \param[out] none
    \retval     none
*/
void TIMER2_IRQHandler(void)
{
    if(timer_interrupt_flag_get(TIMER2, TIMER_INT_CH3) != RESET)
    {
        vIFRPEncodeMachine();

        timer_interrupt_flag_clear(TIMER2, TIMER_INT_CH3);
    }
}

/*!
    \brief      This function handles TIMER interrupt request.
    \param[in]  none
    \param[out] none
    \retval     none
*/
void TIMER3_IRQHandler(void)
{
    uint16_t usTimerCnt = 0;

    if(timer_interrupt_flag_get(TIMER3, TIMER_INT_CH1) != RESET)
    {
        timer_interrupt_flag_clear(TIMER3, TIMER_INT_CH1);

        usTimerCnt = TIMER_CH1CV(TIMER3);
        enumQueuePushDatas(&g_TypeQueueKeyTouch[0], (uint8_t *)&usTimerCnt, sizeof(usTimerCnt));
    }
    if(timer_interrupt_flag_get(TIMER3, TIMER_INT_CH2) != RESET)
    {
        timer_interrupt_flag_clear(TIMER3, TIMER_INT_CH2);

        usTimerCnt = TIMER_CH2CV(TIMER3);
        enumQueuePushDatas(&g_TypeQueueKeyTouch[1], (uint8_t *)&usTimerCnt, sizeof(usTimerCnt));
    }
}

/*!
    \brief      This function handles TIMER interrupt request.
    \param[in]  none
    \param[out] none
    \retval     none
*/
void TIMER5_IRQHandler(void)
{
    if(timer_interrupt_flag_get(TIMER5, TIMER_INT_UP) != RESET)
    {
        timer_interrupt_flag_clear(TIMER5, TIMER_INT_UP);

        /* 溢出值为65536 */
        g_lTimerHigh += 65536;
    }
}

/*!
    \brief      This function handles TIMER interrupt request.
    \param[in]  none
    \param[out] none
    \retval     none
*/
void TIMER15_IRQHandler(void)
{
    if(timer_interrupt_flag_get(TIMER15, TIMER_INT_UP) != RESET)
    {
        timer_interrupt_flag_clear(TIMER15, TIMER_INT_UP);

        /* 软件定时器状态机 1ms 周期执行 */
        cSoftTimerMachine();
    }
}

/*!
    \brief      This function handles TIMER interrupt request.
    \param[in]  none
    \param[out] none
    \retval     none
*/
void TIMER16_IRQHandler(void)
{
    if(timer_interrupt_flag_get(TIMER16, TIMER_INT_UP) != RESET)
    {
        timer_interrupt_flag_clear(TIMER16, TIMER_INT_UP);
    }
}

/*!
    \brief      this function handles USART interrupt request
    \param[in]  none
    \param[out] none
    \retval     none
*/
void USART0_IRQHandler(void)
{
    volatile static uint32_t uiMDANdtrNow = 0;

    if(usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE) != RESET)
    {
        while(g_uiUart0DMAReadCntOld != (uiMDANdtrNow = USART0_DMA_READ_LENGTH - dma_transfer_number_get(DMA1, DMA_CH2)))
        {
            if(uiMDANdtrNow < g_uiUart0DMAReadCntOld)
            {
                /* 把一帧数据读取到UART队列缓存里 */
                enumQueuePushDatas(&g_TypeQueueUart0Read, &g_USART0ReadDMABuff[g_uiUart0DMAReadCntOld], USART0_DMA_READ_LENGTH - g_uiUart0DMAReadCntOld);
                g_uiUart0DMAReadCntOld = 0;
            }

            /* 把一帧数据读取到UART队列缓存里 */
            enumQueuePushDatas(&g_TypeQueueUart0Read, &g_USART0ReadDMABuff[g_uiUart0DMAReadCntOld], uiMDANdtrNow - g_uiUart0DMAReadCntOld);

            g_uiUart0DMAReadCntOld = uiMDANdtrNow;
        }

        /* 发送信号量 */
        rt_sem_release(g_SemReadMessage);
    }
    /* 错误中断 */
    else
    {
        /* 重新初始化 */
        vUart0Init();
    }

    usart_flag_clear(USART0, USART_FLAG_IDLE);
    usart_interrupt_flag_clear(USART0, USART_INT_FLAG_PERR | USART_INT_FLAG_ERR_FERR | USART_INT_FLAG_ERR_NERR | USART_INT_FLAG_RBNE_ORERR | USART_INT_FLAG_ERR_ORERR);
    usart_interrupt_flag_clear(USART0, USART_INT_FLAG_IDLE | USART_INT_FLAG_TC | USART_INT_FLAG_LBD | USART_INT_FLAG_CTS | USART_INT_FLAG_RT);
    usart_interrupt_flag_clear(USART0, USART_INT_FLAG_AM | USART_INT_FLAG_WU);
}

/*!
    \brief      this function handles DMAx channelx interrupt
    \param[in]  none
    \param[out] none
    \retval     none
*/
void DMA1_Channel2_IRQHandler(void)
{
    volatile static uint32_t uiMDANdtrNow = 0;

    if((dma_interrupt_flag_get(DMA1, DMA_CH2, DMA_INT_FLAG_HTF) != RESET) || (dma_interrupt_flag_get(DMA1, DMA_CH2, DMA_INT_FLAG_FTF) != RESET))
    {
        dma_interrupt_flag_clear(DMA1, DMA_CH2, DMA_INT_FLAG_HTF | DMA_INT_FLAG_FTF);

        if(g_uiUart0DMAReadCntOld != (uiMDANdtrNow = USART0_DMA_READ_LENGTH - dma_transfer_number_get(DMA1, DMA_CH2)))
        {
            if(uiMDANdtrNow < g_uiUart0DMAReadCntOld)
            {
                /* 把一帧数据读取到UART队列缓存里 */
                enumQueuePushDatas(&g_TypeQueueUart0Read, &g_USART0ReadDMABuff[g_uiUart0DMAReadCntOld], USART0_DMA_READ_LENGTH - g_uiUart0DMAReadCntOld);
                g_uiUart0DMAReadCntOld = 0;
            }

            /* 把一帧数据读取到UART队列缓存里 */
            enumQueuePushDatas(&g_TypeQueueUart0Read, &g_USART0ReadDMABuff[g_uiUart0DMAReadCntOld], uiMDANdtrNow - g_uiUart0DMAReadCntOld);

            g_uiUart0DMAReadCntOld = uiMDANdtrNow;

            /* 发送信号量 */
            rt_sem_release(g_SemReadMessage);
        }
    }
}

/*!
    \brief      this function handles USART interrupt request
    \param[in]  none
    \param[out] none
    \retval     none
*/
void USART1_IRQHandler(void)
{
    volatile static uint32_t uiMDANdtrNow = 0;

    if(usart_interrupt_flag_get(USART1, USART_INT_FLAG_IDLE) != RESET)
    {
        while(g_uiUart1DMAReadCntOld != (uiMDANdtrNow = USART1_DMA_READ_LENGTH - dma_transfer_number_get(DMA0, DMA_CH5)))
        {
            if(uiMDANdtrNow < g_uiUart1DMAReadCntOld)
            {
                /* 把一帧数据读取到UART队列缓存里 */
                enumQueuePushDatas(&g_TypeQueueUart1Read, &g_USART1ReadDMABuff[g_uiUart1DMAReadCntOld], USART1_DMA_READ_LENGTH - g_uiUart1DMAReadCntOld);
                g_uiUart1DMAReadCntOld = 0;
            }

            /* 把一帧数据读取到UART队列缓存里 */
            enumQueuePushDatas(&g_TypeQueueUart1Read, &g_USART1ReadDMABuff[g_uiUart1DMAReadCntOld], uiMDANdtrNow - g_uiUart1DMAReadCntOld);

            g_uiUart1DMAReadCntOld = uiMDANdtrNow;
        }

        /* 发送信号量 */
        rt_sem_release(g_SemReadMessage);
    }
    /* 错误中断 */
    else
    {
        /* 重新初始化 */
        vUart1Init();
    }

    usart_flag_clear(USART1, USART_FLAG_IDLE);
    usart_interrupt_flag_clear(USART1, USART_INT_FLAG_PERR | USART_INT_FLAG_ERR_FERR | USART_INT_FLAG_ERR_NERR | USART_INT_FLAG_RBNE_ORERR | USART_INT_FLAG_ERR_ORERR);
    usart_interrupt_flag_clear(USART1, USART_INT_FLAG_IDLE | USART_INT_FLAG_TC | USART_INT_FLAG_LBD | USART_INT_FLAG_CTS | USART_INT_FLAG_RT);
    usart_interrupt_flag_clear(USART1, USART_INT_FLAG_AM | USART_INT_FLAG_WU);
}

/*!
    \brief      this function handles DMAx channelx interrupt
    \param[in]  none
    \param[out] none
    \retval     none
*/
void DMA0_Channel5_IRQHandler(void)
{
    volatile static uint32_t uiMDANdtrNow = 0;

    if((dma_interrupt_flag_get(DMA0, DMA_CH5, DMA_INT_FLAG_HTF) != RESET) || (dma_interrupt_flag_get(DMA0, DMA_CH5, DMA_INT_FLAG_FTF) != RESET))
    {
        dma_interrupt_flag_clear(DMA0, DMA_CH5, DMA_INT_FLAG_HTF | DMA_INT_FLAG_FTF);

        if(g_uiUart1DMAReadCntOld != (uiMDANdtrNow = USART1_DMA_READ_LENGTH - dma_transfer_number_get(DMA0, DMA_CH5)))
        {
            if(uiMDANdtrNow < g_uiUart1DMAReadCntOld)
            {
                /* 把一帧数据读取到UART队列缓存里 */
                enumQueuePushDatas(&g_TypeQueueUart1Read, &g_USART1ReadDMABuff[g_uiUart1DMAReadCntOld], USART1_DMA_READ_LENGTH - g_uiUart1DMAReadCntOld);
                g_uiUart1DMAReadCntOld = 0;
            }

            /* 把一帧数据读取到UART队列缓存里 */
            enumQueuePushDatas(&g_TypeQueueUart1Read, &g_USART1ReadDMABuff[g_uiUart1DMAReadCntOld], uiMDANdtrNow - g_uiUart1DMAReadCntOld);

            g_uiUart1DMAReadCntOld = uiMDANdtrNow;

            /* 发送信号量 */
            rt_sem_release(g_SemReadMessage);
        }
    }
}

/*!
    \brief      this function handles USART interrupt request
    \param[in]  none
    \param[out] none
    \retval     none
*/
void USART2_IRQHandler(void)
{
    volatile static uint32_t uiMDANdtrNow = 0;

    if(usart_interrupt_flag_get(USART2, USART_INT_FLAG_IDLE) != RESET)
    {
        while(g_uiUart2DMAReadCntOld != (uiMDANdtrNow = USART2_DMA_READ_LENGTH - dma_transfer_number_get(DMA0, DMA_CH1)))
        {
            if(uiMDANdtrNow < g_uiUart2DMAReadCntOld)
            {
                /* 把一帧数据读取到UART队列缓存里 */
                enumQueuePushDatas(&g_TypeQueueUart2Read, &g_USART2ReadDMABuff[g_uiUart2DMAReadCntOld], USART2_DMA_READ_LENGTH - g_uiUart2DMAReadCntOld);
                g_uiUart2DMAReadCntOld = 0;
            }

            /* 把一帧数据读取到UART队列缓存里 */
            enumQueuePushDatas(&g_TypeQueueUart2Read, &g_USART2ReadDMABuff[g_uiUart2DMAReadCntOld], uiMDANdtrNow - g_uiUart2DMAReadCntOld);

            g_uiUart2DMAReadCntOld = uiMDANdtrNow;
        }

        /* 发送信号量 */
        rt_sem_release(g_SemReadMessage);
    }
    /* 错误中断 */
    else
    {
        /* 重新初始化 */
        vUart2Init();
    }

    usart_flag_clear(USART2, USART_FLAG_IDLE);
    usart_interrupt_flag_clear(USART2, USART_INT_FLAG_PERR | USART_INT_FLAG_ERR_FERR | USART_INT_FLAG_ERR_NERR | USART_INT_FLAG_RBNE_ORERR | USART_INT_FLAG_ERR_ORERR);
    usart_interrupt_flag_clear(USART2, USART_INT_FLAG_IDLE | USART_INT_FLAG_TC | USART_INT_FLAG_LBD | USART_INT_FLAG_CTS | USART_INT_FLAG_RT);
    usart_interrupt_flag_clear(USART2, USART_INT_FLAG_AM | USART_INT_FLAG_WU);
}

/*!
    \brief      this function handles DMAx channelx interrupt
    \param[in]  none
    \param[out] none
    \retval     none
*/
void DMA0_Channel1_IRQHandler(void)
{
    volatile static uint32_t uiMDANdtrNow = 0;

    if((dma_interrupt_flag_get(DMA0, DMA_CH1, DMA_INT_FLAG_HTF) != RESET) || (dma_interrupt_flag_get(DMA0, DMA_CH1, DMA_INT_FLAG_FTF) != RESET))
    {
        dma_interrupt_flag_clear(DMA0, DMA_CH1, DMA_INT_FLAG_HTF | DMA_INT_FLAG_FTF);

        if(g_uiUart2DMAReadCntOld != (uiMDANdtrNow = USART2_DMA_READ_LENGTH - dma_transfer_number_get(DMA0, DMA_CH1)))
        {
            if(uiMDANdtrNow < g_uiUart2DMAReadCntOld)
            {
                /* 把一帧数据读取到UART队列缓存里 */
                enumQueuePushDatas(&g_TypeQueueUart2Read, &g_USART2ReadDMABuff[g_uiUart2DMAReadCntOld], USART2_DMA_READ_LENGTH - g_uiUart2DMAReadCntOld);
                g_uiUart2DMAReadCntOld = 0;
            }

            /* 把一帧数据读取到UART队列缓存里 */
            enumQueuePushDatas(&g_TypeQueueUart2Read, &g_USART2ReadDMABuff[g_uiUart2DMAReadCntOld], uiMDANdtrNow - g_uiUart2DMAReadCntOld);

            g_uiUart2DMAReadCntOld = uiMDANdtrNow;

            /* 发送信号量 */
            rt_sem_release(g_SemReadMessage);
        }
    }
}

/*!
    \brief      this function handles external lines 2 interrupt request
    \param[in]  none
    \param[out] none
    \retval     none
*/
void EXTI5_9_IRQHandler(void)
{
    static int64_t st_NecIRTimeLast = 0;
    int64_t st_NecIRTimeNow = 0;
    int16_t sTime = 0;

    if (RESET != exti_interrupt_flag_get(EXTI_7))
    {
        exti_interrupt_flag_clear(EXTI_7);

        st_NecIRTimeNow = lTimeGetStamp();
        sTime = ((st_NecIRTimeNow - st_NecIRTimeLast) < 0x7FFF) ? (st_NecIRTimeNow - st_NecIRTimeLast) : 0x7FFF;
        st_NecIRTimeLast = st_NecIRTimeNow;

        enumQueuePushDatas(&g_TypeQueueIFRPDecode, (uint8_t *)&sTime, sizeof(sTime));
    }
}

/*!
    \brief      this function handles DMAx channelx interrupt
    \param[in]  none
    \param[out] none
    \retval     none
*/
void DMA0_Channel4_IRQHandler(void)
{
    if(dma_interrupt_flag_get(DMA0, DMA_CH4, DMA_INT_FLAG_FTF) != RESET)
    {
        dma_interrupt_flag_clear(DMA0, DMA_CH4, DMA_INT_FLAG_FTF);

        extern void lcd_refresh(void);
        lcd_refresh();
    }
}

/*!
    \brief      this function handles USBFS interrupt
    \param[in]  none
    \param[out] none
    \retval     none
*/
void USBFS_IRQHandler (void)
{
    extern usb_core_driver msc_udisk;
    usbd_isr(&msc_udisk);
}

/*!
    \brief      this function handles SDIO interrupt request
    \param[in]  none
    \param[out] none
    \retval     none
*/
void SDIO_IRQHandler(void)
{
    sd_interrupts_process();
}

/*!
    \brief      this function handles SPI1 exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void SPI1_IRQHandler(void)
{
    static int32_t iCntNow = 0;

    if(spi_i2s_interrupt_flag_get(SPI1,SPI_I2S_INT_TBE) != RESET)
    {
        iCntNow = (iCntNow >= PLAY_BUFFER_SIZE) ? 0 : iCntNow;

        spi_i2s_data_transmit(SPI1, g_usIISPalyDatas[iCntNow++]);
    }
}
